//
//  OSTabBar.m
//  百思不得姐
//
//  Created by gl on 16/2/19.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSTabBar.h"
#import "OSPublishViewController.h"
@interface OSTabBar ()


@property (nonatomic,weak)UIButton *publishButton;

@end




@implementation OSTabBar


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
        
        UIButton *publishButton = [[UIButton alloc]init];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        self.publishButton = publishButton;

        
        [self.publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.publishButton];
    }
    
    
    return self;
    
}
-(void)publishClick{
    OSPublishViewController *publish = [[OSPublishViewController alloc]init];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publish animated:NO completion:nil];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
//    self.publishButton.frame = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
  
    
    
    
    self.publishButton.size = self.publishButton.currentBackgroundImage.size;
      self.publishButton.center = CGPointMake(OSScreenW / 2, self.height / 2);

    
    CGFloat buttonY = 0;
    CGFloat buttonW = self.width / 5;
    CGFloat buttonH = self.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
//        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (![button isKindOfClass:[UIControl class]] || button == self.publishButton) {
                continue;
            }
        
            CGFloat buttonX = buttonW * ((index > 1) ? (index + 1):index);
            button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
            
            index++;
    }
}

@end
